package com.example.jm24;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

public class IntroduceActivity1 extends AppCompatActivity {
    private ImageView iv;
    private TextView tv,tv2,tv3,tv4,tv5;
    private Button btn1,btn2;
    ProgressDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce1);
        Bundle b=getIntent().getExtras();
        //获取Bundle的信息
        final String info=b.getString("name");
        final String info1=b.getString("housename");
        final String info2=b.getString("username");
        final String url=b.getString("url");
        findviews();
        insert(info,info1,info2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UserDao ud = new UserDao();
                        final Boolean res=ud.Refund(info1,info2);
                            Handler mHandler = new Handler(Looper.getMainLooper()) {

                                @Override
                                public void handleMessage(@NonNull Message msg) {
                                    super.handleMessage(msg);
                                    // 切换到主线程
                                    if (!res) {
                                        //Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                        AlertDialog.Builder builder = new AlertDialog.Builder(IntroduceActivity1.this);//必须用IntroduceActivity1.this//getContext报错
                                        builder.setTitle("退房信息");
                                        builder.setMessage("退房成功");
                                        builder.setCancelable(false);
                                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //Intent intent = new Intent(getApplication(), IntroduceActivity1.class);
                                                //startActivity(intent);
                                            }
                                        });
                                        builder.show();
                                        //Looper.loop();
                                        //show();
                                    }
                                }
/*
                                private void show() {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(IntroduceActivity1.this);
                                    builder.setTitle("退房信息");
                                    builder.setMessage("退房成功");
                                    builder.setCancelable(false);
                                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                           // Intent intent = new Intent(IntroduceActivity1.this, IntroduceActivity1.class);
                                            //startActivity(intent);
                                            //dialog.dismiss();
                                        }
                                    });
                                    builder.show();
                                }*/
                            };
                            mHandler.sendEmptyMessage(0);
                            //dialog.dismiss();
                                            /*Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                                            builder.setTitle("退房信息");
                                            builder.setMessage("退房成功");
                                            builder.setCancelable(false);
                                            builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    Intent intent = new Intent(getApplicationContext(),IntroduceActivity1.class);
                                                    startActivity(intent);
                                                }
                                            });
                                            builder.show();
                                            Looper.loop();
                                            /*
                                            Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                            Toast toast = Toast.makeText(getApplicationContext(), "已退房！", Toast.LENGTH_SHORT);
                                            toast.show();
                                            Looper.loop();*/
                    }
                }).start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Comment.class);
                Bundle bundle = new Bundle();
                bundle.putString("name", info);
                bundle.putString("housename", info1);
                bundle.putString("username", info2);
                bundle.putString("url", url);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    private void insert(final String info, final String info1, final String info2) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                //Bundle b=getIntent().getExtras();
                //String info=b.getString("name");
                System.out.println(info);
                UserDao d = new UserDao();
                final Data get = d.insert(info1);
                Handler mHandler = new Handler(Looper.getMainLooper()) {

                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        // 切换到主线程
                        //DataAdapter mAdapter = new DataAdapter((LinkedList<Data>) mData, mContext);//初始化适配器对象
                        //list.setAdapter(mAdapter);//设置适配器
                        Glide.with(IntroduceActivity1.this)
                                .load("http://" + get.getUrl()).diskCacheStrategy(DiskCacheStrategy.NONE)//磁盘不缓存
                                .skipMemoryCache(true)
                                .into(iv);
                        tv.setText("￥:" + get.getPrice()+"/天");
                        tv2.setText(get.getType());
                        tv3.setText(get.getArea()+"㎡");
                        tv4.setText(get.getToward());
                        tv5.setText(get.getIntroduce());
                        /*
                        btn1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        UserDao ud = new UserDao();
                                        Boolean res=ud.Refund(info1,info2);
                                        if (!res) {
                                            Handler mHandler = new Handler(Looper.getMainLooper()) {

                                                @Override
                                                public void handleMessage(@NonNull Message msg) {
                                                    super.handleMessage(msg);
                                                    // 切换到主线程
                                                    //Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                                                    builder.setTitle("退房信息");
                                                    builder.setMessage("退房成功");
                                                    builder.setCancelable(false);
                                                    builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                                                        @Override
                                                        public void onClick(DialogInterface dialogInterface, int i) {
                                                            Intent intent = new Intent(getApplicationContext(),IntroduceActivity1.class);
                                                            startActivity(intent);
                                                        }
                                                    });
                                                    builder.show();
                                                    //Looper.loop();
                                                    builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                                                        @Override
                                                        public void onClick(DialogInterface dialogInterface, int i) {
                                                            Intent intent = new Intent(getApplicationContext(),IntroduceActivity1.class);
                                                            startActivity(intent);
                                                        }
                                                    });
                                                    builder.show();
                                                    Looper.loop();
                                                }
                                            };
                                            mHandler.sendEmptyMessage(0);
                                            //dialog.dismiss();
                                            /*Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                                            builder.setTitle("退房信息");
                                            builder.setMessage("退房成功");
                                            builder.setCancelable(false);
                                            builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    Intent intent = new Intent(getApplicationContext(),IntroduceActivity1.class);
                                                    startActivity(intent);
                                                }
                                            });
                                            builder.show();
                                            Looper.loop();
                                            /*
                                            Looper.prepare();//非主线程中没有开启Looper，而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环，调用Looper.loop()来使消息循环起作用
                                            Toast toast = Toast.makeText(getApplicationContext(), "已退房！", Toast.LENGTH_SHORT);
                                            toast.show();
                                            Looper.loop();
                                        } else {
                                                Looper.prepare();
                                                Toast toast = Toast.makeText(getApplicationContext(), "退房失败", Toast.LENGTH_SHORT);
                                                toast.show();
                                                Looper.loop();
                                        }
                                    }
                                }).start();
                            }
                        });*/

                    }
                };
                mHandler.sendEmptyMessage(0);
            }
        }.start();
    }

    private void findviews() {
        iv=findViewById(R.id.iv);
        tv=findViewById(R.id.tv);
        tv2=findViewById(R.id.tv2);
        tv3=findViewById(R.id.tv3);
        tv4=findViewById(R.id.tv4);
        tv5=findViewById(R.id.tv5);
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
    }
}
